import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/resource_path.dart';
import 'package:tender_flutter/page/home/tender_list_page.dart';
import 'package:tender_flutter/router/router.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/widgets/search_app_bar3.dart';

class SearchListPage extends StatefulWidget {
  /// 标讯类型
  final String tenderType;

  /// 搜索关键词
  final String keyword;

  /// 根据关键词是否精确查询
  final exactQuery;

  /// 是否显示城市选项
  final bool isShowCity;

  const SearchListPage(
      {Key key, this.keyword, this.exactQuery = false, this.tenderType, this.isShowCity})
      : super(key: key);

  @override
  _SearchListPageState createState() => _SearchListPageState();
}

class _SearchListPageState extends State<SearchListPage> {
  String _keyword = "";

  GlobalKey<TenderListPageState> _listKey = GlobalKey();

  @override
  void initState() {
    super.initState();
    _keyword = widget.keyword;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: _initAppBar(), body: _initBody());
  }

  /// 导航
  Widget _initAppBar() {
    return SearchAppBarStyle3(
      isShowCity: widget.isShowCity,
        onSearch: (word, areaId) {
          _keyword = word;
          _listKey.currentState.areaCode = areaId;
          _listKey.currentState.keyword = _keyword;
          _listKey.currentState.updateData(true);
        },
        autoFocus: _keyword != null ? false : true,
        value: _keyword,
        leading: FlatButton(
            onPressed: () {
              LRouter.navigatorCtx(context).pop();
            },
            splashColor: Colors.transparent,
            highlightColor: Colors.transparent,
            child: Image.asset(
              RPath.imgPath + "icon_nav_back_balck.png",
              fit: BoxFit.contain,
              width: ScreenAdapter.width(30),
              height: ScreenAdapter.width(30),
            )));
  }

  /// 内容体
  Widget _initBody() {
    if (_keyword != null && _keyword.isNotEmpty) {
      return TenderListPage(
        key: _listKey,
        tenderType: widget.tenderType,
        keyword: _keyword,
        exactQuery: widget.exactQuery,
      );
    } else {
      return Container();
    }
  }
}
